package com.elong.businesstravel.protocol.hotel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.client.HttpResponseException;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Environment;

import com.android.support.jhf.utils.FileUtils;
import com.elong.businesstravel.entitys.HotelPrice;
import com.elong.businesstravel.entitys.HotelPrice.BookingRuleInfo;
import com.elong.businesstravel.entitys.HotelPrice.GuaranteeRuleInfo;
import com.elong.businesstravel.entitys.HotelPrice.PrepayRuleInfo;
import com.elong.businesstravel.entitys.HotelPrice.RatePlanInfo;
import com.elong.businesstravel.entitys.HotelPrice.RoomDesction;
import com.elong.businesstravel.entitys.HotelPrice.RoomInfo;
import com.elong.businesstravel.protocol.baseresponse.BaseELongJsonHttpResponse;

public class GetHotelPriceResponse extends BaseELongJsonHttpResponse {

	public HotelPrice hotelPrice = new HotelPrice();

	@Override
	public void onSuccess(int statusCode, Header[] headers,
			JSONObject mainJsonObject) throws Throwable {

//		FileUtils.saveFile(mainJsonObject.toString(), new File(Environment
//				.getExternalStorageDirectory().getAbsolutePath()
//				+ "/hotelprice.json"));

		JSONObject responseJsonObject = mainJsonObject
				.optJSONObject("response");
		if (null == responseJsonObject) {
			return;
		}
		JSONArray roomsJsonArray = responseJsonObject.optJSONArray("Rooms");
		if (null == roomsJsonArray) {
			return;
		}
		for (int i = 0; i < roomsJsonArray.length(); i++) {
			JSONObject itemRoomsObject = roomsJsonArray.optJSONObject(i);
			if (null != itemRoomsObject) {
				RoomInfo roomInfo = new RoomInfo();
				roomInfo.RoomName = itemRoomsObject.optString("RoomName", "");
				roomInfo.RoomId = itemRoomsObject.optString("RoomId", "");
				roomInfo.image = itemRoomsObject.optString("image", "");
				roomInfo.desctionString = itemRoomsObject.optString("desction",
						"");
				if (roomInfo.desctionString.equalsIgnoreCase("False")) {
					roomInfo.desction = null;
				} else {

					JSONObject desctionJsonObject = itemRoomsObject
							.optJSONObject("desction");
					if (null != desctionJsonObject) {
						RoomDesction roomDesction = new RoomDesction();
						roomDesction.roomarea = desctionJsonObject.optString(
								"roomarea", "");
						roomDesction.broadnetaccess = desctionJsonObject
								.optString("broadnetaccess", "");
						roomDesction.bedtype = desctionJsonObject.optString(
								"bedtype", "");
						roomDesction.broadnetfee = desctionJsonObject
								.optString("broadnetfee", "");
						roomDesction.roomfloor = desctionJsonObject.optString(
								"roomfloor", "");
						roomDesction.comments = desctionJsonObject.optString(
								"comments", "");
						roomDesction.description = desctionJsonObject
								.optString("description", "");
						roomInfo.desction = roomDesction;
					} else {
						roomInfo.desction = null;
					}
				}

				JSONArray ratePlansJsonArray = itemRoomsObject
						.optJSONArray("RatePlans");
				if (null != ratePlansJsonArray) {
					for (int j = 0; j < ratePlansJsonArray.length(); j++) {
						JSONObject itemRatePlansObject = ratePlansJsonArray
								.optJSONObject(j);
						if (null != itemRatePlansObject) {
							RatePlanInfo ratePlanInfo = new RatePlanInfo();
							ratePlanInfo.RoomName = itemRatePlansObject
									.optString("RoomName", ""); // 房型名称
							ratePlanInfo.desction = roomInfo.desction;
							// public String image; //

							ratePlanInfo.RoomTypeId = itemRatePlansObject
									.optString("RoomTypeId", ""); // 房型id
							ratePlanInfo.AverageRate = itemRatePlansObject
									.optString("AverageRate", ""); // 单价
							ratePlanInfo.TotalRate = itemRatePlansObject
									.optString("TotalRate", ""); // 总价
							ratePlanInfo.Bonus = itemRatePlansObject.optString(
									"Bonus", ""); // 返现
							ratePlanInfo.TotalBonus = itemRatePlansObject
									.optString("TotalBonus", ""); // 总返现
							ratePlanInfo.PaymentType = itemRatePlansObject
									.optString("PaymentType", ""); // Prepay预付
							ratePlanInfo.CustomerType = itemRatePlansObject
									.optString("CustomerType", ""); // 宾客类型
							ratePlanInfo.RateId = itemRatePlansObject
									.optString("RateId", ""); // 产品编号
							ratePlanInfo.nrpname = itemRatePlansObject
									.optString("nrpname", ""); //
							ratePlanInfo.RateName = itemRatePlansObject
									.optString("RateName", ""); // 产品名称
							ratePlanInfo.Status = itemRatePlansObject
									.optString("Status", ""); // 状态 false--不可销售
							ratePlanInfo.BreakFast = itemRatePlansObject
									.optString("BreakFast", ""); // 早餐
							ratePlanInfo.MinBooking = itemRatePlansObject
									.optString("MinBooking", ""); // 最小预订间数
							ratePlanInfo.MaxBooking = itemRatePlansObject
									.optString("MaxBooking", ""); // 最大预订间数
							ratePlanInfo.GuaranteeRuleIds = itemRatePlansObject
									.optString("GuaranteeRuleIds", ""); // 担保id（有值则需要担保）

							ratePlanInfo.PrepayRuleIds = itemRatePlansObject
									.optString("PrepayRuleIds", ""); // 预付id
							// public List<GuaranteeRuleInfo> GuaranteeRules =
							// new ArrayList<HotelPrice.GuaranteeRuleInfo>(); //
							// 担保规则
							JSONArray prepayRulesJsonArray = itemRatePlansObject
									.optJSONArray("PrepayRules");
							if (null != prepayRulesJsonArray) {
								for (int k = 0; k < prepayRulesJsonArray
										.length(); k++) {
									JSONObject itemPrepayRulesObject = prepayRulesJsonArray
											.optJSONObject(k);
									if (null != itemPrepayRulesObject) {
										PrepayRuleInfo prepayRuleInfo = new PrepayRuleInfo();
										prepayRuleInfo.Description = itemPrepayRulesObject
												.optString("Description", "");
										ratePlanInfo.PrepayRules
												.add(prepayRuleInfo);
									}
								}
							}

							JSONArray bookingRuleArray = itemRatePlansObject
									.optJSONArray("BookingRules");
							if (null != bookingRuleArray) {
								for (int k = 0; k < bookingRuleArray.length(); k++) {
									BookingRuleInfo bookingRuleInfo = new BookingRuleInfo();
									JSONObject itemBookingRule = bookingRuleArray
											.optJSONObject(k);
									if (null != itemBookingRule) {
										bookingRuleInfo.Description = itemBookingRule
												.optString("Description", "");
										bookingRuleInfo.TypeCode = itemBookingRule
												.optString("TypeCode", "");
										ratePlanInfo.BookingRules
												.add(bookingRuleInfo);
									}
								}
							}

							JSONArray guaranteeRuleInfoArray = itemRatePlansObject
									.optJSONArray("GuaranteeRules");
							if (null != guaranteeRuleInfoArray) {
								for (int k = 0; k < guaranteeRuleInfoArray
										.length(); k++) {
									GuaranteeRuleInfo guaranteeRuleInfo = new GuaranteeRuleInfo();
									JSONObject itemGuarantee = guaranteeRuleInfoArray
											.optJSONObject(k);
									if (null != itemGuarantee) {
										guaranteeRuleInfo.StartDate = itemGuarantee.optString("StartDate");
										guaranteeRuleInfo.EndDate = itemGuarantee.optString("EndDate");
										guaranteeRuleInfo.DateType = itemGuarantee.optString("DateType","");
										guaranteeRuleInfo.WeekSet = itemGuarantee.optString("WeekSet","");
										guaranteeRuleInfo.Amount = itemGuarantee
												.optString("Amount", ""); // 房间数
										guaranteeRuleInfo.Description = itemGuarantee
												.optString("Description", "");
										guaranteeRuleInfo.StartTime = itemGuarantee
												.optString("StartTime", ""); // 到店时间
										guaranteeRuleInfo.EndTime = itemGuarantee
												.optString("EndTime", ""); //
										guaranteeRuleInfo.GuaranteeType = itemGuarantee
												.optString("GuaranteeType", ""); // 担保金类型
																					// FirstNightCost为首晚房费担保
										// FullNightCost为全额房费担保
										guaranteeRuleInfo.IsAmountGuarantee = itemGuarantee
												.optString("IsAmountGuarantee",
														""); // 房间担保
										guaranteeRuleInfo.IsTimeGuarantee = itemGuarantee
												.optString("IsTimeGuarantee",
														""); // 到店时间担保
										ratePlanInfo.GuaranteeRules
												.add(guaranteeRuleInfo);
									}
								}
							}

							roomInfo.RatePlans.add(ratePlanInfo);
						}
					}
				}

				hotelPrice.mRoomInfos.add(roomInfo);
			}
		}

	}

}
